//
//  CCImgBrowserController.swift
//  KastKing
//
//  Created by liujianlong on 2023/1/5.
//

import UIKit
import SnapKit
#if canImport(TCC_String)
import TCC_String
#endif
#if canImport(TCC_Extension)
import TCC_Extension
#endif
#if canImport(TCC_View)
import TCC_View
#endif
#if canImport(TCC_ViewController)
import TCC_ViewController
#endif
/// 图片全屏浏览
@objc open class CCImgBrowserController : UIViewController {
    open var collectionView: UICollectionView!
    open var index = 0
    open override func viewDidLoad() {
        super.viewDidLoad()
        self.initUI()
    }
    open override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.view.backgroundColor = "#000000".color(1)
    }
    
    open override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        self.collectionView.isHidden = false
        self.collectionView.scrollToItem(at: IndexPath(row:self.index, section:0), at: UICollectionView.ScrollPosition.centeredHorizontally, animated: false)
    }
    open func initUI() {
        self.collectionView = UICollectionView(frame: .zero, collectionViewLayout: UICollectionViewLayout())
        self.collectionView.delegate = self
        self.collectionView.dataSource = self
        self.collectionView.isHidden = true
        self.collectionView.backgroundColor = "#000000".color(0.8)
        self.collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "cell")
        self.view.addSubview(self.collectionView)
        self.collectionView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        let tap = UITapGestureRecognizer(target: self, action: #selector(actionForTap))
        tap.cancelsTouchesInView = false
        self.view.addGestureRecognizer(tap)
    }
    @objc open var imgs = [UIImage](){
        didSet{
            self.collectionView.reloadData()
        }
    }
    
    @objc private func actionForTap(){
        self.view.endEditing(true)
    }
    
    open override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        guard self.firstResponder(self.view) != nil else{
            let touch : UITouch = (touches as NSSet).allObjects.last as! UITouch
            if touch.view?.isDescendant(of: self.view.subviews.first ?? UIView()) == false{
                self.dismiss(animated: false)
            }
            return }
        super.touchesEnded(touches, with: event)
        self.view.endEditing(true)
    }
}
//MARK: - UICollectionViewDataSource
extension CCImgBrowserController : UICollectionViewDataSource
{
    public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.imgs.count
    }
    public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier:"cell", for: indexPath)
        cell.contentView.removeAllSubView()
        let img = UIImageView()
        img.image = self.imgs[indexPath.row]
        img.contentMode = .scaleAspectFit
        cell.contentView.addSubview(img)
        img.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.centerY.equalToSuperview()
            make.top.bottom.greaterThanOrEqualToSuperview()
        }
        return cell
    }
}
//MARK: - UICollectionViewDelegate
extension CCImgBrowserController : UICollectionViewDelegate
{
    public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        self.dismiss(animated: false)
    }
}

//MARK: - UICollectionViewDelegateFlowLayout
extension CCImgBrowserController : UICollectionViewDelegateFlowLayout{
    public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize{
        let size = UIScreen.main.bounds.size
        return CGSize(width:size.width, height:size.height)
    }
    
    public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat{
        return 0
    }
    
    public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat{
        return 0
    }
    public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets{
        return UIEdgeInsets(top:0, left: 0, bottom: 0, right: 0)
    }
}
